<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>

import AuthorizeMasterplate from "@/components/AuthorizeMasterplate"
import unauthorizedMasterplate from "@/components/unauthorizedMasterplate"

import edit from "../edit/home/index";
import updateRecordEdit from "../edit/updateRecord/index";
import experienceEdit from "../edit/experience/index";

export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base],
  components: {
    AuthorizeMasterplate,
    unauthorizedMasterplate,
  },
  data() {
    return {
      appWindow: window,
      load: false,
      showType: "",
      
      wxappData: {},
      
      apiUrl: {
        wxappEmpower: "mall-site-portal/wechat-open-config/getOpenPreAuthUrl",
        wxappEmpowerDetails: "mall-site-portal/wechat-open-config/getAuthorizerInfo",
        wxappDetails: "mall-site-portal/wechat-ma-audit/getAuditDetails",
        examineRevoke: "mall-site-portal/wechat-ma-audit/undoCodeAudit",
        publishWxapp: "mall-site-portal/wechat-ma-audit/releaseAudited",
      },
      
      authType_id: 2,
    };
  },
  methods: {
    // 初始化
    init() {
      let _auth_code = this.$route.query.auth_code;

      if(_auth_code){
        // 解析
      let _d = encodeURI(_auth_code).split("@@@");
        this.getAuthorizeNews(encodeURI(_d[1]));
      }else{
        this.getData();
      }

    },

    // 验证小程序是否有授权,获取小程序详情
   async getData(){
     this.load = true;

     let _data = await this.$vFN.Http.post(
                {
                    url: this.apiUrl.wxappDetails,
                },
        window
      );

      if (_data._ok) {
        
        // 判断是否有小程序ID
        // isWechatOpen;平台 0 否 1 是
        if(_data.data.appId &&  _data.data.isWechatOpen){
           this.showType = '2';
           this.wxappData = _data.data;
           
        }else{
          this.showType = '1';
        }

      }

      this.load = false;
    },

    // 授权
   async authorize(){
   
      let url = window.location.href;
   
      let _data = await this.$vFN.Http.post(
                {
                    url: this.apiUrl.wxappEmpower,
                    data: {
                      authType: this.authType_id,
                      redirectUri: url
                    }
                },
        window
      );

      if(_data._ok){
       window.open( _data.data)
      }
      
    },

   // 根据AuthCode获取授权信息
   async getAuthorizeNews(value){
      this.load = true;

       let _data = await this.$vFN.Http.post(
                {
                    url: this.apiUrl.wxappEmpowerDetails,
                    data: {
                      authType: this.authType_id,
                      authCode: value,
                    }
                },
        window
      );

      if(_data._ok){
        this.getData();
      }

    this.load = false;
   },

  // 列表按钮
  clickGranted(value){
     switch(value){
        case '1': 
            this.authorize();
           break;
         default:
     }
  },

  // 小程序详情
   clickButton(value){
     // update:更新 again:重新授权 trial:提审版本  experience：体检版选择 revoke:审核撤销 publish:发布小程序
    switch(value){
      case 'update': 
          updateRecordEdit.init({
              mixinEdit_editTitle: "更新记录",
          })
          break;
      case 'again': 
          this.authorize();
          break;
      case 'trial': 
          edit.init({
              mixinEdit_editTitle: "提审版本",
              isEditionList: this.wxappData.templateDetails.maTemplateList,
              onSuccess: (res) => {
                this.getData();
              }
          })
          break;
       case 'experience':
             experienceEdit.init({
              mixinEdit_editTitle: "体检版",
              isEditionList: this.wxappData.templateDetails.maTemplateList,
          })
            break;
      case 'revoke':
          this.examineRevoke(this.wxappData.auditDetails.templateId);
        break;
      case 'publish':
          this.publishWxapp(this.wxappData.auditDetails.templateId);
        break;
      default:
        break;
    }

   },

    // 审核撤销
   async examineRevoke(value){

     let _data = await this.$vFN.Http.post(
                {
                    url: this.apiUrl.examineRevoke,
                    data: {
                      templateId: value,
                    }
                },
        window
      );

      if(_data._ok){
        this.getData();
      }

    },

    // 发布小程序
    async publishWxapp(value){

      let _data = await this.$vFN.Http.post(
                {
                    url: this.apiUrl.publishWxapp,
                    data: {
                      templateId: value,
                    }
                },
        window
      );

      if(_data._ok){
         this.$vFN.Tools.messageSuccess("发布成功");
         await this.getData();
      }

    },

  },
  mounted() {
    this.init();
  },
  activated() {
  },
};
</script>